$(document).ready(function(){
//bootbox.alert("Hello world!", function() {
////Example.show("Hello world callback");
//}); 

});

function URL(){
    var siteURL2 = $('body').data('siteurl');
    return siteURL2;
}

//funciones de CRUD
$('#tabla').on('click', '#agregar', function(event){
    event.preventDefault();
    var siteURL = URL();
    var fila = $(this).parent().parent();
    var nombre = fila.children('td').children('input[name="rol_name"]');
    var nombreValue = nombre.val();
    var $form = $('form').serializeArray();
    $form.push({name:'rol_name',value:nombreValue});
    
    if (nombreValue !== ""){
        $.ajax({
            url: siteURL+'/users/roles',
            data: $form,
            type: 'post',
            success: function(data){
                setNewToken();
                $( "#nuevo" ).after( data.html );
            }
        });
    }
});

$('#tabla').on('click', '.eliminar', function(event){
    event.preventDefault();
    var fila =  $(this).parents('tr');
    var name = fila.children('.cel2').text();
//    var msg = "¿Desea dejar sin rol a los usuarios pertenecientes" +
//            "al rol " + name;
//    bootbox.confirm( msg, function(result) {
//    
//    }); 
    var siteURL = URL();
    var id = $(this).attr( "id_rol" );
    var $form = $('form').serializeArray();
    $form.push({name:'id',value:id});
    
    $.ajax({
        url: siteURL+'/users/roles',
        data: $form,
        type: 'delete',
        success: function(data){
            setNewToken();
            if(data.estatus === true){
                fila.remove();
            }
        }
    });
    
});

$('#tabla').on('click', '.modificar', function(event){
    event.preventDefault();
    var siteURL = URL();
    var fila = $(this).parent().parent();
    var nombre = fila.children('td.cel2').text();
    
    var input = '<input class="form-control"'+
                'type="text"'+
                'placeholder="Nombre del rol"'+
                'value="'+nombre+'" />';
    
    fila.children('td.cel2').html(input);
    fila.children('td.cel3').children('a.modificar').text('Guardar');
    fila.children('td.cel3').children('a.modificar').removeClass( "modificar" ).addClass( "guardar" );;
});

$('#tabla').on('click', '.guardar', function(event){
    event.preventDefault();
    var siteURL = URL();
    var fila = $(this).parent().parent();
    var id = $(this).attr( "id_rol" );
    var nombre = fila.children('td.cel2').children('input').val();
    var $form = $('form').serializeArray();
    $form.push({name:'id',value:id});
    $form.push({name:'rol_name',value:nombre});
    
    $.ajax({
        url: siteURL+'/users/roles',
        data: $form,
        type: 'put',
        success: function(data){
            setNewToken();
            if(data.estatus === true){
                fila.children('td.cel2').html(nombre);
                fila.children('td.cel3').children('a.guardar').text('Modificar');
                fila.children('td.cel3').children('a.guardar').removeClass( "guardar" ).addClass( "modificar" );
            }
        }
    });
});
    
$('#tabla').on('click', '.permissions', function(event){
    event.preventDefault();
    //var $form = $('form').serialize();
    var $id =  $(this).parents('tr').attr( "id" );
    $.ajax({
        url : URL()+'/users/role_permissions/'+$id,
        type : 'GET',
        //data : $form,
        success : function(data){
            $.colorbox({
                html:data.html,
                innerWidth: 250,
                innerHeight: 600,
                maxWidth: "95%",
                maxHeight: "95%"
            });
        }
    });
});

$('body').on('submit', '#formRoles', function(e){
    //este es cuando guarda
    e.preventDefault();
    var $form = $(this);
    var $data = $form.serialize();
    var $action = $form.attr( "action" );
    $.ajax({
        url : $action,
        type : 'POST',
        data : $data,
        success : function(data){
            setNewToken();
            if(data.response === 2){
                manager_user.saveOk();
            } else if (data.response === 1){
                $('#cboxLoadedContent').html(data.html);
            }
        }
    });
});

var manager_user = {
  saveOk: function(){
      $('#cboxLoadedContent').html('Guardado ok');
      setTimeout(function(){ $(window).colorbox.close(); }, 700);
      search();
  }
    
};